function [x1, slicexyb] = APEuvvisslice(datax, datay,wl,nx,ny,ascii,mode)
% 
% function APEuvvisslice(datax,datay,wl,nx,ny,ascii)
%   datasetname : Filename string, extension '.mat' is assumed
%                 Need to have run uvvisload first
%   wl : Low wavelength of bin slice [nm]
%   nx : Number of CCD pixels to bin (if 0, no bin)
%   ny : Number of time slices to bin (if 0, no bin)
%   ascii : if ==1 data is saved as ASCII, TAB delimited
%   mode : if 'r' labels reflect raman, if  'a' (or nil) absorbance
% 
% Bin kinetic slice of ANDOR ASCII kinetic dataset, then plot
% 
% Example:
%   >> uvvisslice('p450_20100707_run1', 10, 50, 413)

% Help if called without argument
if nargin<3 || nargin >7
    help uvvisslice
    return
elseif nargin==3,
    nx=0;
    ny=0;
    ascii=0;
    mode = 'a';
elseif nargin==4;
    ny=0;
    ascii=0;
    mode = 'a';
elseif nargin==5,
    ascii=0;
    mode = 'a';
elseif nargin==6,
    mode = 'a';
end

datax=datax';
datay=datay';

ix=find(datax<=wl, 1, 'last' );  % Find pixel belonging to slice wavelength

if nx ~= 0 && nx ~= 1
    slicex=mean(datay(:,ix:ix+nx-1),2); % Bin kinetic slice
elseif nx == 1
    error('You can not bin one CCD pixel. Choose nx=0 to skip this step')
    return
else
    slicex=datay(:,ix);
end

if ny ~= 0 && ny ~= 1
    slicexyb=rowbin(slicex,ny,1); % Bin time points
    x1=ny*(1:length(slicexyb));
elseif ny == 1
    error('You can not bin one time slice. Choose ny=0 to skip this step')
    return
else
    slicexyb=slicex;
    x1=(1:length(slicexyb));
end

figure(9)

 xlabels{1} = 'Time Slice';
 xlabels{2} = 'time (s)';
 ylabels{1} = 'AU';
 ylabels{2} = 'AU';
axis tight
 
plot(x1, slicexyb)
if mode == 'a'
    xlabel('Time slice [Accumulation]')
    ylabel('Absorbance')
    title(['Kinetic slice wl = ',num2str(wl),' nm, xbin ',num2str(nx),', ybin ',num2str(ny)])
elseif mode == 'r'
    xlabel('Time slice [Accumulation]')
    ylabel('Counts')
    title(['Kinetic slice wl = ',num2str(wl),' cm^-^1, xbin ',num2str(nx),', ybin ',num2str(ny)])
end



function AvgF = rowbin(a,nbin,ncol)
% From
% http://stackoverflow.com/questions/1787057/how-should-i-perform-this-binning-and-averaging-in-matlab

nRemove = rem(size(a,1),nbin); % Find the number of points to remove
a = a(1:end-nRemove,:);        % Trim points to make an even multiple of nbin
Avg = mean(reshape(a,nbin,[],ncol));
AvgF = squeeze(Avg);
